package cn.changeforyou.web.dao.mybatis.ext.utils;

import cn.changeforyou.base.exception.ExceptionFactory;
import cn.changeforyou.web.dao.mybatis.ext.exception.DaoExceptionEnum;
import org.apache.ibatis.binding.MapperMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhyu
 * @version 1.0
 * @date 2022/3/11 9:00
 */
public class MybatisUtil {




    /**
     * 获取并校验唯一一个参数
     * @param runParam
     * @return
     */
    public static Object getAndValidOnlyParam(Object runParam) {
        Object onlyParam = getOnlyParam(runParam);
        if (null == onlyParam) {
            throw ExceptionFactory.jsonException(DaoExceptionEnum.CODE_DESIGN_ESTIMATE_NOT_ENOUGH);
        }
        return onlyParam;
    }

    /**
     * 获取唯一一个参数
     *
     * @param runParam
     * @return
     */
    public static Object getOnlyParam(Object runParam) {
        if (null == runParam) {
            return null;
        }
        if (runParam instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) runParam;
            if (map.containsKey("collection")) {
                return map.get("collection");
            } else {
                return map.get("param1");
            }
        } else {
            return runParam;
        }
    }

    /**
     * 根据类对象获取mybatis的入参
     *
     * @param runParam
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T getParamByClass(Object runParam, Class<T> tClass) {
        if (null == runParam) {
            return null;
        }
        if (runParam instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) runParam;
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                if (tClass.isAssignableFrom(entry.getValue().getClass())) {
                    return (T) entry.getValue();
                }
            }
        } else {
            if (tClass.isAssignableFrom(runParam.getClass())) {
                return (T) runParam;
            }
        }
        return null;
    }

    /**
     * 根据类对象获取并校验mybatis的入参
     *
     * @param runParam
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T getAndValidParamByClass(Object runParam, Class<T> tClass) {
        T param = getParamByClass(runParam, tClass);
        if (null == param) {
            throw ExceptionFactory.jsonException(DaoExceptionEnum.CODE_DESIGN_ESTIMATE_NOT_ENOUGH);
        }
        return param;
    }

    /**
     * 根据参数名字获取并校验mybatis的入参
     * @param runParam
     * @param name
     * @return
     */
    public static  Object getAndValidParamByName(Object runParam, String name) {
        Object param = getParamByName(runParam, name);
        if (null == param) {
            throw ExceptionFactory.jsonException(DaoExceptionEnum.CODE_DESIGN_ESTIMATE_NOT_ENOUGH);
        }
        return param;
    }

    /**
     * 根据参数名字获取并校验mybatis的入参
     * @param runParam
     * @param name
     * @return
     */
    public static Object getParamByName(Object runParam, String name) {
        if (null == runParam) {
            return null;
        }
        if (runParam instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) runParam;
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                if (entry.getKey().equals(name)) {
                    return entry.getValue();
                }
            }
        }else if(runParam instanceof HashMap) {
            HashMap<String, Object> map = ((HashMap<String, Object>) runParam);
            for (Map.Entry<String, ?> entry : map.entrySet()) {
                if (entry.getKey().equals(name)) {
                    return entry.getValue();
                }
            }
        }
        return null;
    }

}
